<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { 
  Tickets, 
  List, 
  Location, 
  Van, 
  Grid, 
  User, 
  SwitchButton,
  DataAnalysis,
  Document
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const activeMenu = ref('')

const handleMenuSelect = (key: string) => {
  activeMenu.value = key
  router.push(key)
}

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

const showLayout = computed(() => {
  return route.path !== '/login'
})

onMounted(() => {
  userStore.restoreUser()
})
</script>

<template>
  <div class="app-container">
    <!-- 登录页面不显示布局 -->
    <router-view v-if="!showLayout" />
    
    <!-- 主应用布局 -->
    <el-container v-else>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="logo">
          <h2>火车票售票系统</h2>
        </div>
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          @select="handleMenuSelect"
          router
        >
          <!-- 购票页面（所有用户可见） -->
          <el-menu-item index="/purchase">
            <el-icon><Tickets /></el-icon>
            <span>购票系统</span>
          </el-menu-item>
          
          <!-- 订单管理（所有用户可见） -->
          <el-menu-item index="/orders">
            <el-icon><List /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          
          <!-- 管理员菜单 -->
          <template v-if="userStore.isAdmin()">
            <el-menu-item index="/stations">
              <el-icon><Location /></el-icon>
              <span>车站管理</span>
            </el-menu-item>
            <el-menu-item index="/trains">
              <el-icon><Van /></el-icon>
              <span>车次管理</span>
            </el-menu-item>
            <el-menu-item index="/carriages">
              <el-icon><Grid /></el-icon>
              <span>车厢管理</span>
            </el-menu-item>
            <el-menu-item index="/ticket-management">
              <el-icon><Document /></el-icon>
              <span>票务管理</span>
            </el-menu-item>
            <el-menu-item index="/users">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/statistics">
              <el-icon><DataAnalysis /></el-icon>
              <span>系统统计</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 头部 -->
        <el-header class="app-header">
          <div class="header-content">
            <span>欢迎，{{ userStore.currentUser?.realName }}</span>
            <el-button type="primary" link @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-button>
          </div>
        </el-header>
        
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
.app-container {
  height: 100vh;
}

.logo {
  padding: 20px;
  text-align: center;
  background-color: #001529;
  color: white;
}

.logo h2 {
  margin: 0;
  font-size: 16px;
}

.sidebar-menu {
  height: calc(100vh - 80px);
  border-right: none;
}

.el-aside {
  background-color: #001529;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 200px;
}

.app-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
